#include "Inventory.h"

//					INVENTAIRE

Inventory::Inventory() {
	clothNb = 0;
	clueNb = 0;
}

void Inventory::init(){
	hud = StageManager::getInstance().getCurrentStage()->getHud();	
}

Inventory::~Inventory() {
}

void Inventory::addClue(int id, std::string fileName) {
	osg::ref_ptr<osg::Geometry> geom = drawPics(fileName, osg::Vec3(1010., 910. - 100.*clueNb, -1.));
	hud->getGeode()->addDrawable(geom);
	m_mclues.insert(std::pair<int,osg::ref_ptr<osg::Geometry> >(id,geom));
	++clueNb;
}

void Inventory::removeClue(int id) {
	std::map<int,osg::ref_ptr<osg::Geometry> >::iterator it = m_mclues.find(id);
	if (it != m_mclues.end()){	
		hud->getGeode()->removeDrawable((*it).second);
		m_vgivenClues.push_back((*it).first);
		m_mclues.erase(it);
	}
}


void Inventory::addCloth(int id, std::string fileName) {
	osg::ref_ptr<osg::Geometry> geom = drawPics(fileName, osg::Vec3(1110., 910. - 100.*clothNb, -1.));
	hud->getGeode()->addDrawable(geom);
	m_mclothes.insert(std::pair<int,osg::ref_ptr<osg::Geometry> >(id,geom));
	++clothNb;
	if(clothNb == 4){
		hud->endGame("YOU WIN! You found all your clothes!");
	}
}

bool Inventory::hasCloth(int id) {
	std::map<int,osg::ref_ptr<osg::Geometry> >::iterator it = m_mclothes.find(id);
	if ( it != m_mclothes.end()){
		return true;
	}
	return false;
}

bool Inventory::hasClue(int id) {
	std::map<int,osg::ref_ptr<osg::Geometry> >::iterator it = m_mclues.find(id);
	if ( it != m_mclues.end()){
		return true;
	}
	return false;
}

bool Inventory::gaveClue(int id) {
	for(unsigned int i=0; i<m_vgivenClues.size(); i++) {
		if (m_vgivenClues[i] == id){
			return true;
		}
	}
	return false;
}

void Inventory::removeCloth(int id) {
	std::map<int,osg::ref_ptr<osg::Geometry> >::iterator it = m_mclothes.find(id);
	if ( it != m_mclothes.end()){
		hud->getGeode()->removeDrawable((*it).second);
		m_mclothes.erase(it);	
		--clothNb;
	}
}

osg::ref_ptr<osg::Geometry> Inventory::drawPics(std::string fileName, osg::Vec3 pos){
	// Texture
	osg::ref_ptr<osg::Texture2D> KLN89FaceTexture = new osg::Texture2D;
	KLN89FaceTexture->setDataVariance(osg::Object::DYNAMIC); 
	// Download picture
	osg::Image* klnFace = osgDB::readImageFile(fileName);
	if (!klnFace)
	{
		std::cout << " couldn't find texture, quiting." << std::endl;
		exit(0);
	}else
		std::cout << "cloth texture downloaded" << std::endl;
	// Assign picture to texture 
	KLN89FaceTexture->setImage(klnFace);

	// Create small square

	int width = 70;
	int height = 70;
	osg::ref_ptr<osg::Geometry> geom;
	geom = hud->drawRectangle(pos,width,height,osg::Vec4(1.0f,1.0,1.0f,1.0f));

	// Apply texture
	osg::ref_ptr<osg::Vec2Array> texcoords = new osg::Vec2Array(4);
	(*texcoords)[0].set(0.f,1.f);
	(*texcoords)[1].set(0.00f,0.0f); 
	(*texcoords)[2].set(1.f,0.0f); 
	(*texcoords)[3].set(1.f,1.f);
	geom->setTexCoordArray(0,texcoords);

	 // Create a new StateSet with default settings: 
	osg::ref_ptr<osg::StateSet> stateOne = new osg::StateSet();

	// Assign texture unit 0 of our new StateSet to the texture and enable the texture.
	stateOne->setTextureAttributeAndModes(0,KLN89FaceTexture,osg::StateAttribute::ON);
	
	// Associate this state set with the Geodm
	geom->setStateSet(stateOne);

	return geom;
}